package com.kaka.bus.engine.repay.detail;

import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.RepayInfo;
import com.kaka.bus.bean.Single;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.DataManager;
import com.kaka.bus.manager.Result;
import com.kaka.bus.manager.T;
import com.kaka.bus.mvp.RxPresenter;
import com.kaka.bus.util.logger.Logger;

import javax.inject.Inject;

/**
 * Created by OnionMac on 2018/1/18.
 */

public class RepayDetailPresenter extends RxPresenter<RepayDetailContract.View> implements RepayDetailContract.Presenter{

    @Inject
    public RepayDetailPresenter(DataManager dataManager) {
        super(dataManager);
    }

    @Override
    public void getRepayInfo(String userId, String token) {

        mView.showDialog("加载付款信息");

        addSubscribe(mDataManager
                .getApi()
                .getRepayInfo(userId,token)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<RepayInfo>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<RepayInfo> repayInfoHttpWrapper) {
                        mView.getInfoSuccess(repayInfoHttpWrapper);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }
                }));
    }

    @Override
    public boolean sendyzm(String phone, String tokenKey,String bankNum) {

        Logger.i("手机号码"+phone);
        /**
         * 发送验证码  发送成功时 应该记住当前发送的号码 防止用户更改号码
         */
        mView.showDialog("发送验证码...");
        addSubscribe(mDataManager
                .getApi()
                .getPayCode(phone,tokenKey,"sendDynamic", Constant.PLATFORM,bankNum)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<Single>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<Single> wrapper) {
                        mView.getCodeSuccess(wrapper);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }

                    @Override
                    protected void onFaild() {
                        super.onFaild();
                        mView.faild();
                    }
                }));
        return true;
    }

    @Override
    public void commitRepayInfo(String phone,String bankId, String bankNum, String borrNum, String optAmout, String triggerStyle,int type,String validateCode) {
        mView.showDialog("认证付款中...");
        addSubscribe(mDataManager
                .getApi()
                .repay(phone,bankId,bankNum,borrNum,optAmout,triggerStyle,type,validateCode)
                .compose(T.D())
                .subscribeWith(new Result<HttpWrapper<String>>() {
                    @Override
                    protected void onSuccess(HttpWrapper<String> wrapper) {
                        mView.showRepayResult(wrapper);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        mView.dissDialog();
                    }
                }));
    }
}
